Rhys Hewer
REMEMBER!! Combine the data sets at the end!
#load libraries
library(readxl)
library(dplyr)
library(ggplot2)
library(plotly)
library(RColorBrewer)
library(corrplot)
library(caret)
library(tidyr)
library(kableExtra)
library(parallel)
library(doParallel)
library(rattle)
library(rpart)
#read in data
setwd("C:/Users/rhysh/Google Drive/Data Science/Ubiqum/Project 2/Task 2")
incomplete <- read.csv("SurveyIncomplete.csv")
origdata <- read_xlsx("Survey_Key_and_Complete_Responses_excel.xlsx", sheet = 2)
data <- origdata
As we are working across 2 spreadsheets it is important to check to see if they are structured alike or whether manipulation will be required to ensure they have the same features.
#Check structure of data to see if alike
names(incomplete) == names(data)
## [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE
The features match between the spreadsheets so no data manipulation is needed in that respect.
incomplete %>% sample_n(5)
## salary age elevel car zipcode credit brand
## 270 22229.85 29 4 12 2 437096.86 0
## 2262 99080.50 80 2 2 3 75020.14 0
## 820 106879.76 73 3 6 8 306411.80 0
## 3669 71122.16 48 3 12 4 332028.44 0
## 4830 138649.26 43 2 5 7 23902.55 0
data %>% sample_n(5)
## # A tibble: 5 x 7
## salary age elevel car zipcode credit brand
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 137576. 50 4 15 3 2000. 1
## 2 114157. 33 0 15 8 328351. 1
## 3 92876. 57 3 16 7 232789. 1
## 4 126217. 30 2 3 7 395842. 1
## 5 125200. 66 0 7 2 277273. 1
A quick look at a sample from both spreadsheets shows that they are very similar in composition.
str(incomplete)
## 'data.frame': 5000 obs. of 7 variables:
## $ salary : num 110500 140894 119160 20000 93956 ...
## $ age : int 54 44 49 56 59 71 32 33 32 58 ...
## $ elevel : int 3 4 2 0 1 2 1 4 1 2 ...
## $ car : int 15 20 1 9 15 7 17 17 19 8 ...
## $ zipcode: int 4 7 3 1 1 2 1 0 2 4 ...
## $ credit : num 354724 395015 122025 99630 458680 ...
## $ brand : int 0 0 0 0 0 0 0 0 0 0 ...
str(data)
## Classes 'tbl_df', 'tbl' and 'data.frame': 10000 obs. of 7 variables:
## $ salary : num 119807 106880 78021 63690 50874 ...
## $ age : num 45 63 23 51 20 56 24 62 29 41 ...
## $ elevel : num 0 1 0 3 3 3 4 3 4 1 ...
## $ car : num 14 11 15 6 14 14 8 3 17 5 ...
## $ zipcode: num 4 6 2 5 4 3 5 0 0 4 ...
## $ credit : num 442038 45007 48795 40889 352951 ...
## $ brand : num 0 1 0 1 0 1 1 1 0 1 ...
Looking at the structure shows that a few changes are needed in respect of the data types.
Overall, however, the datasets are sufficiently similar. I will take the strategy of splitting into training, test and final prediction sets. The training and testing sets will come from the complete responses data. Final prediction from the incomplete responses.
The exploratory data analysis will be performed on the complete data but any data transformations taking place on the training/testing sets will also need to be made on the Final prediction data prior to modelling this.
#check for NAs
data %>% is.na() %>% sum()
## [1] 0
incomplete %>% is.na() %>% sum()
## [1] 0
There are no missing values in either data set.
#data types
data$elevel <- data$elevel %>% as.factor()
data$car <- data$car %>% as.factor()
data$zipcode <- data$zipcode %>% as.factor()
data$brand <- data$brand %>% as.factor()
Education level, car owned, zip code and brand preference are converted from numeric to factors.
##check for outliers
numericVars <- Filter(is.numeric, data)
outliers <- numericVars %>% sapply(function(x) boxplot(x, plot=FALSE)$out) %>% str()
## List of 3
## $ salary: num(0)
## $ age : num(0)
## $ credit: num(0)
There are no outliers.
#EDA
##Key feature is brand preference, begin with exploring this value.
g6 <- ggplot(data, aes(brand, fill = brand)) +
geom_bar() +
theme_bw() +
scale_fill_brewer(palette="Dark2") +
xlab("Brand Preference") +
ylab("Frequency") +
ggtitle("Brand Preference Frequencies")
g6
##review other histograms for skewdness
histData <- origdata %>% select(-brand)
g8 <- ggplot(gather(histData), aes(value)) +
geom_histogram(bins = 10, fill = "#D95F02", colour = "white") +
theme_bw() +
facet_wrap(~key, scales = 'free_x') +
xlab("Value") +
ylab("Count") +
ggtitle("Histograms of Numeric Variables")
g8
## Plot Brand choice v other variables
g1 <- ggplot(data, aes(brand, salary, fill = brand)) +
geom_violin() +
theme_bw() +
scale_fill_brewer(palette="Dark2") +
xlab("Brand Preference") +
ylab("Salary ($)") +
ggtitle("Brand Preference v Salary")
g1 <- ggplotly(g1)
g1
g2 <- ggplot(data, aes(brand, age, fill = brand)) +
geom_violin() +
theme_bw() +
scale_fill_brewer(palette="Dark2") +
xlab("Brand Preference") +
ylab("Age") +
ggtitle("Brand Preference v Age")
g2 <- ggplotly(g2)
g2
g3 <- ggplot(data, aes(brand, elevel, colour = brand)) +
geom_count() +
theme_bw() +
scale_color_brewer(palette="Dark2") +
xlab("Brand Preference") +
ylab("Education Level") +
ggtitle("Brand Preference v Education Level")
g3
g4 <- ggplot(data, aes(brand, car, colour = brand)) +
geom_count() +
theme_bw() +
scale_color_brewer(palette="Dark2") +
xlab("Brand Preference") +
ylab("Car") +
ggtitle("Brand Preference v Car")
g4
g5 <- ggplot(data, aes(brand, zipcode, colour = brand)) +
geom_count() +
theme_bw() +
scale_color_brewer(palette="Dark2") +
xlab("Brand Preference") +
ylab("Zip Code") +
ggtitle("Brand Preference v Zip Code")
g5
g7 <- ggplot(data, aes(brand, credit, fill = brand)) +
geom_violin() +
theme_bw() +
scale_fill_brewer(palette="Dark2") +
xlab("Brand Preference") +
ylab("Credit") +
ggtitle("Brand Preference v Credit")
g7 <- ggplotly(g7)
g7
There is a general preference for brand 1 and salary seems the key feature.
Age and credit seem to have very limited influence on brand preference. Whilst there are patterns of preference within education level, car and zip code, salary appears to have the most striking impact.
Salaries ranging between 45k - 100k seem to have a preference for brand 0. Salaries outside of this range seem to have a preference for brand 1.
We can review the initial hypothesis and provide some information on which to make feature selection decisions using a decision tree.
featureDT <- rpart(brand ~ ., data = data)
featureDT$variable.importance
## age salary car zipcode credit elevel
## 2036.06398 1421.30552 78.59978 42.70644 21.20404 16.06770
fancyRpartPlot(featureDT)
The decision tree is formed exclusively of age and salary, suggesting these are the key features. This is reinforced by the variable importance information from within the model.
g12 <- ggplot(data, aes(salary, age, colour = brand)) +
geom_point(show.legend = FALSE) +
facet_grid(brand ~ .) +
theme_bw() +
scale_color_brewer(palette="Dark2") +
xlab("Salary") +
ylab("Age") +
ggtitle("Salary v Age by Brand Preference")
g12
We see that the interaction of age and salary do play a role in brand preference. There are clearly defined blocks of preference depending on age and salary. For example, between ages 60-80 earning between 25k-80k have a distinct preference for brand 0.
I have updated the initial hypothesis to now consider that age and salary are the key features in determining brand preference.
##Review correlation matrix
corrMatrix <- origdata %>% cor()
corrMatrix %>% corrplot.mixed()
Reviewing the correlation plot we see very little correlation between the features. This means that colinearity is not an issue that needs to be addressed.
#near zero variance
nzv <- data %>% nearZeroVar(saveMetrics = TRUE)
nzv
## freqRatio percentUnique zeroVar nzv
## salary 1.112069 97.53 FALSE FALSE
## age 1.178744 0.61 FALSE FALSE
## elevel 1.035946 0.05 FALSE FALSE
## car 1.026415 0.20 FALSE FALSE
## zipcode 1.020105 0.09 FALSE FALSE
## credit 1.057377 97.51 FALSE FALSE
## brand 1.643405 0.02 FALSE FALSE
There are no features with near zero variance.
The Random Forest algorithm has built-in feature selection so no feature selection required from that perspective.
For KNN I will take both a fully-featured dataset and a concentrated dataset (brand, salary, age) into the modelling phase and compare results.
#Creating Testing/Training sets
set.seed(111)
trainIndex <- createDataPartition(iris$Species, p = 0.75, list = FALSE)
training <- data[ trainIndex,]
testing <- data[-trainIndex,]
concData <- data %>% select(brand, age, salary)
training.conc <- concData[ trainIndex,]
testing.conc <- concData[-trainIndex,]
#set up parallel processing (requires parallel and doParallel libraries)
cluster <- makeCluster(detectCores() - 1)
registerDoParallel(cluster)
#Cross Validation 10 fold
fitControl<- trainControl(method = "cv", number = 10, savePredictions = TRUE, allowParallel = TRUE)
A test and traing set are created with all features. An additional training set is created with just Brand, Age and Salary. 10-fold cross validation is being used and parallel processing is being leveraged.
# Deploy KNN model
model.KNN.brand <- train(brand ~ ., data = training,
method = "knn", trControl = fitControl)
model.KNN.brand
## k-Nearest Neighbors
##
## 114 samples
## 6 predictor
## 2 classes: '0', '1'
##
## No pre-processing
## Resampling: Cross-Validated (10 fold)
## Summary of sample sizes: 102, 103, 103, 102, 103, 102, ...
## Resampling results across tuning parameters:
##
## k Accuracy Kappa
## 5 0.7386364 0.2455904
## 7 0.7196970 0.1720068
## 9 0.7015152 0.1015749
##
## Accuracy was used to select the optimal model using the largest value.
## The final value used for the model was k = 5.
# Deploy KNN model with concentrated feature set
model.KNN.brand.conc <- train(brand ~ ., data = training.conc,
method = "knn", trControl = fitControl)
model.KNN.brand.conc
## k-Nearest Neighbors
##
## 114 samples
## 2 predictor
## 2 classes: '0', '1'
##
## No pre-processing
## Resampling: Cross-Validated (10 fold)
## Summary of sample sizes: 103, 103, 103, 103, 102, 103, ...
## Resampling results across tuning parameters:
##
## k Accuracy Kappa
## 5 0.7346154 0.2830504
## 7 0.7451049 0.3028544
## 9 0.7298368 0.2675434
##
## Accuracy was used to select the optimal model using the largest value.
## The final value used for the model was k = 7.
# Deploy KNN model with scaled features
model.KNN.brand.scale <- train(brand ~ ., data = training,
method = "knn", trControl = fitControl,
preProcess = c("center", "scale"))
model.KNN.brand.scale
## k-Nearest Neighbors
##
## 114 samples
## 6 predictor
## 2 classes: '0', '1'
##
## Pre-processing: centered (34), scaled (34)
## Resampling: Cross-Validated (10 fold)
## Summary of sample sizes: 103, 103, 102, 102, 103, 103, ...
## Resampling results across tuning parameters:
##
## k Accuracy Kappa
## 5 0.6674242 -0.03808089
## 7 0.6750000 -0.05051582
## 9 0.6931818 -0.04586466
##
## Accuracy was used to select the optimal model using the largest value.
## The final value used for the model was k = 9.
# Deploy KNN model with scaled features and concentrated feature set
model.KNN.brand.conc.scale <- train(brand ~ ., data = training.conc,
method = "knn", trControl = fitControl,
preProcess = c("center", "scale"))
model.KNN.brand.conc.scale
## k-Nearest Neighbors
##
## 114 samples
## 2 predictor
## 2 classes: '0', '1'
##
## Pre-processing: centered (2), scaled (2)
## Resampling: Cross-Validated (10 fold)
## Summary of sample sizes: 103, 103, 103, 103, 102, 101, ...
## Resampling results across tuning parameters:
##
## k Accuracy Kappa
## 5 0.8679487 0.6380495
## 7 0.8581002 0.6001548
## 9 0.8246503 0.4795492
##
## Accuracy was used to select the optimal model using the largest value.
## The final value used for the model was k = 5.
#tune best KNN model further
tGridKNN <- expand.grid(k = c(1,2,3,4,5,6))
finalKNN <- train(brand ~ ., data = training.conc,
method = "knn", trControl = fitControl,
preProcess = c("center", "scale"),
tuneGrid = tGridKNN)
finalKNN
## k-Nearest Neighbors
##
## 114 samples
## 2 predictor
## 2 classes: '0', '1'
##
## Pre-processing: centered (2), scaled (2)
## Resampling: Cross-Validated (10 fold)
## Summary of sample sizes: 101, 103, 103, 103, 103, 102, ...
## Resampling results across tuning parameters:
##
## k Accuracy Kappa
## 1 0.9113636 0.7810864
## 2 0.9030303 0.7682660
## 3 0.9303030 0.8325011
## 4 0.8688228 0.6379981
## 5 0.9036713 0.7356803
## 6 0.8597319 0.5987915
##
## Accuracy was used to select the optimal model using the largest value.
## The final value used for the model was k = 3.
By tuning the model I was able to increase the accuracy on the training sent to 91% and the Kappa to 0.78. This brings concerns of overfitting.
#Predictions on the test set (model = model name, testing = test set)
predictions.KNN <- predict(finalKNN, testing)
testing$predictions.KNN <- predictions.KNN
#Confusion matrix
confMatrix <- confusionMatrix(testing$brand, testing$predictions.KNN)
confMatrix
## Confusion Matrix and Statistics
##
## Reference
## Prediction 0 1
## 0 3056 695
## 1 588 5547
##
## Accuracy : 0.8702
## 95% CI : (0.8634, 0.8768)
## No Information Rate : 0.6314
## P-Value [Acc > NIR] : < 2.2e-16
##
## Kappa : 0.7229
## Mcnemar's Test P-Value : 0.003083
##
## Sensitivity : 0.8386
## Specificity : 0.8887
## Pos Pred Value : 0.8147
## Neg Pred Value : 0.9042
## Prevalence : 0.3686
## Detection Rate : 0.3091
## Detection Prevalence : 0.3794
## Balanced Accuracy : 0.8636
##
## 'Positive' Class : 0
##
fourfoldplot(confMatrix$table, conf.level = 0, margin = 1, main = "Confusion Matrix")
The Accuracy of the model on the test set was 87% with a kappa of 0.72. These seem to be satisfactory outcomes. Reviewing the confusion matrix we see that the model is doing a fairly good job of classifying brand preference with a slight tendency to overpredict brand 0.
# Deploy RF model
tGridRF <- expand.grid(mtry = c(24,30,35))
model.RF.brand.tune <- train(brand ~ ., data = training,
method = "rf", trControl = fitControl,
tuneGrid = tGridRF)
model.RF.brand.tune
## Random Forest
##
## 114 samples
## 6 predictor
## 2 classes: '0', '1'
##
## No pre-processing
## Resampling: Cross-Validated (10 fold)
## Summary of sample sizes: 102, 103, 102, 102, 103, 103, ...
## Resampling results across tuning parameters:
##
## mtry Accuracy Kappa
## 24 0.7734848 0.3644853
## 30 0.7833333 0.4161136
## 35 0.7833333 0.4161136
##
## Accuracy was used to select the optimal model using the largest value.
## The final value used for the model was mtry = 30.
## Deploy RF model on concentrated feature set
model.RF.brand.conc <- train(brand ~ ., data = training.conc,
method = "rf", trControl = fitControl)
## note: only 1 unique complexity parameters in default grid. Truncating the grid to 1 .
model.RF.brand.conc
## Random Forest
##
## 114 samples
## 2 predictor
## 2 classes: '0', '1'
##
## No pre-processing
## Resampling: Cross-Validated (10 fold)
## Summary of sample sizes: 103, 103, 103, 103, 103, 103, ...
## Resampling results:
##
## Accuracy Kappa
## 0.89662 0.7457665
##
## Tuning parameter 'mtry' was held constant at a value of 2
varImp(model.RF.brand.conc)
## rf variable importance
##
## Overall
## salary 100
## age 0